﻿using System;
using System.Collections.Generic;

using System.Text;
using Estimate.Common.Repository;
using System.Text.RegularExpressions;
using Estimate.Common.Entity;
using Estimate.Common.DBFieldTypes;

namespace Estimate.Common.DataImporter
{
    /// <summary>
    /// 数据导入类的工厂
    /// 根据要求，为调用对象组装出一个导入类。导入类只需要再调用 IDataImporter接口中的函数即可。
    /// </summary>
    public class DataImporterFactory
    {
        /// <summary>
        /// 获取城镇报，预报站点列表
        /// </summary>
        /// <param name="connectionString"></param>
        /// <returns></returns>
        private static string[] getNormalFilterStations(string connectionString)
        {
            RealStationCompareRepository repository = new RealStationCompareRepository(connectionString);
            return repository.GetAllForeStationIds(RealStationCompareType.Normal_Auto);            
        }

        /// <summary>
        /// 获取指导，预报站点列表
        /// </summary>
        /// <param name="connectionString"></param>
        /// <param name="type"></param>
        /// <returns></returns>
        private static string[] getSiteFilterStations(string connectionString)
        {
            RealStationCompareRepository repository = new RealStationCompareRepository(connectionString);
            return repository.GetAllForeStationIds(RealStationCompareType.Site_Auto);
        }

        private static string[] getAutoFilterStations(string connectionString)
        {
            RealStationCompareRepository repository = new RealStationCompareRepository(connectionString);
            return repository.GetAllRealStationIds(RealStationCompareType.Normal_Auto);
        }

        private static string[] getManualFilterStations(string connectionString)
        {
            RealStationCompareRepository repository = new RealStationCompareRepository(connectionString);
            return repository.GetAllRealStationIds(RealStationCompareType.Normal_Manual);
        }

        private static Dictionary<string, string> getForeRealStationCompare(string connectionString, RealStationCompareType type)
        {
            RealStationCompareRepository repository = new RealStationCompareRepository(connectionString);
            return repository.GetForeRealStationCompare(type);
        }

        /// <summary>
        /// 获取 自动站实况数据 导入类
        /// </summary>
        /// <param name="connectionString">数据库连接字符串</param>
        /// <returns>IDataImporter对象</returns>
        public static IDataImporter GetAutoStationDataImporter(string connectionString)
        {
            RainRealRepository rainRepository = RainRealRepositoryFactory.AutoStationRainRealRepository(connectionString);
            if (!rainRepository.IsTableExist())
                rainRepository.CreateTable();


            TempRealRepository tempRepository = TempRealRepositoryFactory.AutoStationTempRealRepository(connectionString);
            if (!tempRepository.IsTableExist())
                tempRepository.CreateTable();

            RealDataLogRepository logRepository = new RealDataLogRepository(connectionString);

            string[] stations = getAutoFilterStations(connectionString);

            return new AutoStationImporter(rainRepository, tempRepository,logRepository,stations);
        }

        /// <summary>
        /// 获取 手动站实况数据 导入类
        /// </summary>
        /// <param name="connectionString">数据库连接字符串</param>
        /// <returns>IDataImporter对象</returns>
        public static IDataImporter GetManualStationDataImporter(string connectionString)
        {
            RainRealRepository rainRepository = RainRealRepositoryFactory.ManualRainRealRepository(connectionString);
            if (!rainRepository.IsTableExist())
                rainRepository.CreateTable();

            TempRealRepository tempRepository = TempRealRepositoryFactory.ManualTempRealRepository(connectionString);
            if (!tempRepository.IsTableExist())
                tempRepository.CreateTable();

            RealDataLogRepository logRepository = new RealDataLogRepository(connectionString);

            string[] stations = getManualFilterStations(connectionString);

            return new ManualStationImporter(rainRepository, tempRepository, logRepository, stations);
        }

        /// <summary>
        /// 获取 省台指导站预报 文件导入对象
        /// </summary>
        /// <param name="connectionString">数据库连接字符串</param>
        /// <returns>IDataImporter对象</returns>
        public static IDataImporter GetStSiteForeDataImporter(string connectionString)
        {
            ForeRepository repository = ForeRepositoryFactory.StSiteForeRepository(connectionString);
            if (!repository.IsTableExist())
                repository.CreateTable();

            SiteForeImporter importer = new SiteForeImporter(repository, getSiteFilterStations(connectionString));
            return importer;
        }

        /// <summary>
        /// 获取 个人指导站预报 文件导入对象
        /// </summary>
        /// <param name="connectionString">数据库连接字符串</param>
        /// <param name="user">用户名</param>
        /// <returns>IDataImporter对象</returns>
        public static IDataImporter GetUserSiteForeDataImporter(string connectionString,string user)
        {
            ForeRepository repository = ForeRepositoryFactory.SiteForeRepository(connectionString, user);
            if (!repository.IsTableExist())
                repository.CreateTable();

            SiteForeImporter importer = new SiteForeImporter(repository, getSiteFilterStations(connectionString));
            return importer;
        }

        /// <summary>
        /// 获取 城镇预报 文件导入对象
        /// </summary>
        /// <param name="connectionString">数据库连接字符串</param>
        /// <param name="user">用户名</param>
        /// <returns>IDataImporter对象</returns>
        public static NormalCityForeImporter GetCityForeDataImporter(string connectionString)
        {
            ForeRepository repository = ForeRepositoryFactory.NormalCityForeRepository(connectionString);
            if (!repository.IsTableExist())
                repository.CreateTable();

            NormalCityForeImporter importer = new NormalCityForeImporter(repository, getNormalFilterStations(connectionString));
            return importer;
        }


        /// <summary>
        /// 获取 城镇预报 文件导入对象
        /// </summary>
        /// <param name="connectionString">数据库连接字符串</param>
        /// <param name="user">用户名</param>
        /// <returns>IDataImporter对象</returns>
        public static IDataImporter GetSdxCityForeDataImporter(string connectionString)
        {
            ForeRepository repository = ForeRepositoryFactory.SdxCityForeRepository(connectionString);
            if (!repository.IsTableExist())
                repository.CreateTable();



            SdxCityForeImporter importer = new SdxCityForeImporter(repository, getNormalFilterStations(connectionString));
            return importer;
        }

        /// <summary>
        /// 获取 国家局指导预报 文件导入对象
        /// </summary>
        /// <param name="connectionString">数据库连接字符串</param>
        /// <param name="user">用户名</param>
        /// <returns>IDataImporter对象</returns>
        public static ZytCityForeImporter GetZytCityForeDataImporter(string connectionString)
        {
            ForeRepository repository = ForeRepositoryFactory.ZytCityForeRepository(connectionString);           

            if (!repository.IsTableExist())
                repository.CreateTable();
            ZytCityForeImporter importer = new ZytCityForeImporter(repository, getNormalFilterStations(connectionString));
            return importer;
        }

        /// <summary>
        /// 获取 降水等值线预报 文件导入对象
        /// </summary>
        /// <param name="connectionString">数据库连接字符串</param>
        /// <param name="user">用户名</param>
        /// <returns>IDataImporter对象</returns>
        public static IDataImporter GetRainForeDataImporter(string connectionString)
        {
            ForeRepository repository = ForeRepositoryFactory.RainForeRepository(connectionString);
            if (!repository.IsTableExist())
                repository.CreateTable();
            RainForeImporter importer = new RainForeImporter(repository);
            return importer;
        }


        /// <summary>
        /// 获取排班表入库导入对象
        /// </summary>
        /// <param name="connection">数据库连接字符串</param>
        /// <returns>IDataImporter对象</returns>
        public static IDataImporter GetScheduleDataImporter(string connection)
        {
            ScheduleLogRepository logRepository = new ScheduleLogRepository(connection);
            ScheduleRepository repository = new ScheduleRepository(connection);

            if (!logRepository.IsTableExist())
                logRepository.CreateTable();
            if (!repository.IsTableExist())
                repository.CreateTable();

            return new ScheduleImporter(logRepository, repository);
        }
    }
}
